﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DAL;
namespace BLL
{
    /// <summary>
    ///Security 的摘要说明
    /// </summary>
    public class Security
    {
        public Security()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }
        static SecurityEvaluateDataContext db = new SecurityEvaluateDataContext();
        public static DAL.SecurityProblem getProblemByID(string id)
        {
            List<DAL.SecurityProblem> problems = (from pp in db.SecurityProblem where pp.Id == new Guid(id) select pp).ToList();
            if (problems.Count > 0)
            {
                return problems[0];
            }
            else
            {
                return null;
            }
        }

        public static List<DAL.SecurityProblem> getSecurityProblemList(string SubjectID)
        {
            List<DAL.SecurityProblem> list = new List<DAL.SecurityProblem>();
            list = (from pp in db.SecurityProblem where pp.Sid == new Guid(SubjectID) select pp).ToList();
            return list;
        }

        public static DAL.SecuritySubject getSubjectByID(string id)
        {
            List<DAL.SecuritySubject> problems = (from pp in db.SecuritySubject where pp.Id == new Guid(id) select pp).ToList();
            if (problems.Count > 0)
            {
                return problems[0];
            }
            else
            {
                return null;
            }
        }
        public static DAL.SecurityOption getOptionByID(string id)
        {
            List<DAL.SecurityOption> problems = (from pp in db.SecurityOption where pp.Id == new Guid(id) select pp).ToList();
            if (problems.Count > 0)
            {
                return problems[0];
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 当前用户是否有题目
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <returns></returns>
        public static List<DAL.Examination> getSubjectByUserID(string userName)
        {
            DateTime Now = DateTime.Now;
            List<DAL.Examination> examList = new List<DAL.Examination>();
            Yipusoft.Account.Model.UserInfo user = Yipusoft.Account.BLL.User.GetUser(userName);
            if (user != null)
            {
                DAL.SecurityEvaluateDataContext db = new SecurityEvaluateDataContext();
                var cc = from o in db.Examination where (DateTime.Now >= o.StartTime && DateTime.Now <= o.EndTime) && o.DepartmentID.Trim() == user.DepartmentID.Trim() select o;
                examList = cc.ToList();
                examList = examList.Where(o => !(from aa in db.SecurityResult where aa.SoldierID == user.UserID select aa.Sid.Value).Contains(o.SID.Value)).ToList();

            }
            return examList;

        }

        public static List<DAL.SecuritySubject> getsubjectLogsByUserID(string userName)
        {
            List<DAL.SecuritySubject> list = new List<DAL.SecuritySubject>();
            Yipusoft.Account.Model.UserInfo user = Yipusoft.Account.BLL.User.GetUser(userName);
            List<Guid> pp = (from aa in db.SecurityResult where aa.SoldierID == user.UserID group aa by aa.Sid into p select p.Key.Value).ToList();
            list = (from cc in db.SecuritySubject where pp.Contains(cc.Id) select cc).ToList();
            return list;
        }

        public static DAL.Examination getExamationBySubject(Guid sid, string UserID)
        {
            DAL.Examination exam = null;
            DAL.SecurityEvaluateDataContext db = new SecurityEvaluateDataContext();
            Yipusoft.Account.Model.UserInfo user = Yipusoft.Account.BLL.User.GetUser(UserID);
            if (user != null)
            {
                var pp = from aa in db.Examination where aa.DepartmentID == user.DepartmentID && aa.SID.Value == sid select aa;
                exam = pp.FirstOrDefault();
            }
            return exam;


        }

        public static DAL.SecurityResult getResult(string userID, Guid sid, Guid Pid)
        {
            DAL.SecurityEvaluateDataContext db = new SecurityEvaluateDataContext();
            var pp = from aa in db.SecurityResult where aa.Sid == sid && aa.SoldierID == userID && aa.Pid == Pid select aa;
            return pp.FirstOrDefault();
        }

        public static DateTime? getLastResultDate(string userID, Guid sid)
        {
            DAL.SecurityEvaluateDataContext db = new SecurityEvaluateDataContext();
            var pp = from aa in db.SecurityResult where aa.Sid == sid && aa.SoldierID == userID orderby aa.SetTime descending select aa.SetTime;
            return pp.FirstOrDefault();
        }
    }
}